package com.financing.wap.web.controller.api;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.common.Session;
import com.financing.wap.dto.ScheduleDTO;
import com.financing.wap.dto.TaskDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IScheduleService;
import com.financing.wap.util.Constants;
import com.financing.wap.web.controller.BaseController;

/**
 * 任务管理/日程管理
 * @author localadmin
 *
 */

@Controller
@RequestMapping(value="/api/schedule")
public class ScheduleController extends BaseController {
	
	@Autowired
	public IScheduleService scheduleService;
	
	@RequestMapping(value="/{id}")
	@ResponseBody
	public ResultMap list(@PathVariable int id){
		ResultMap result = new ResultMap();
		TaskDTO task = new TaskDTO();
		task.setId(id);
		task.setFaId(getSession(true).getUser().getId());
		task = scheduleService.queryTaskById(task);
		
		result.setStatus(STATUS.SUCCESS);
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		result.setAttribute("task", task);
		return result;
	}
	
	
	@RequestMapping(value="/list")
	@ResponseBody
	public ResultMap list(Pager pager, ScheduleDTO schedule){
		ResultMap result = new ResultMap();
		Session session = super.getSession(true);
		UserDTO user = session.getUser();
		schedule.setFaId(user.getId());
		schedule = queryVoChange(schedule);
		List<ScheduleDTO> list = scheduleService.list(pager, schedule);
		result.setStatus(STATUS.SUCCESS);
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		result.setMessage("查询日程列表成功");
		result.setAttribute("list", list);
		result.setAttribute("pager", pager);
		return result;
	}
	
	@RequestMapping(value="/input")
	@ResponseBody
	public ResultMap input(@Validated TaskDTO task, BindingResult br){
		
		ResultMap result = new ResultMap();
//		if(br.hasErrors()){
//			result.setStatus(STATUS.FAILE);
//			result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_VALIDATE_ERROR);
//			result.setMessage("参数校验失败");
//			return result;
//		}
		
		Session session = super.getSession(true);
		UserDTO user = session.getUser();
		if(!session.getLogin() || user == null){
			//登陆
			result.setStatus(STATUS.NEED_LOGIN);
			result.setMessage("需要登陆");
			return result;
		}
		task.setFaId(user.getId());
		boolean success = scheduleService.add(task);
		if(success){
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("添加日程成功");
			return result;
		}else{
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setStatus(STATUS.FAILE);
			result.setMessage("添加日程失败");
			return result;
		}
	}
	
	/**
	 * 取消日程任务
	 * @param task
	 * @return
	 */
	@RequestMapping(value="/cancel")
	@ResponseBody
	public ResultMap cancel(TaskDTO task){
		ResultMap result = new ResultMap();
		task.setStatus(Constants.SCHEDULE_STATUS_CANCEL);
		boolean returnResult = scheduleService.updateTaskStatus(task);
		if(returnResult){
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("取消日程成功");
		}else{
			result.setStatus(STATUS.FAILE);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setMessage("取消日程失败");
		}
		return result;
	}
	
	/**
	 * 取消日程任务
	 * @param task
	 * @return
	 */
	@RequestMapping(value="/complete")
	@ResponseBody
	public ResultMap complete(TaskDTO task){
		ResultMap result = new ResultMap();
		
		task.setStatus(Constants.SCHEDULE_STATUS_SUCCESS);
		boolean returnResult = scheduleService.updateTaskStatus(task);
		if(returnResult){
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("完成日程成功");
		}else{
			result.setStatus(STATUS.FAILE);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setMessage("完成日程失败");
		}
		return result;
	}
	
	/**
	 * 对查询条件进行处理
	 * @param queryVo
	 * @return
	 */
	private ScheduleDTO queryVoChange(ScheduleDTO schedule){
		Date day = schedule.getDay();//schedule.getExecuteTimeEnd()
		Date executeTimeEnd = null;
		if(day!=null){
			
			Calendar c = Calendar.getInstance();
			c.setTime(day);
			c.add(Calendar.DAY_OF_MONTH, 1);
			schedule.setExecuteTimeEnd(c.getTime());
			return schedule;
		}
		executeTimeEnd = schedule.getExecuteTimeEnd();
		if(executeTimeEnd!=null){
			Calendar c = Calendar.getInstance();
			c.setTime(executeTimeEnd);
			c.add(Calendar.DAY_OF_MONTH, 1);
			schedule.setExecuteTimeEnd(c.getTime());
		}
		return schedule;
	}
}
